<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 后台登陆管理
 * @property Admin_mdl $admin
 */
class Login extends CI_Controller {
	
	const CAPTCHA_SESSION_KEY = 'hs_mng_captcha_code';

	public function __construct(){
		parent::__construct();
		if(!isset($_SESSION)){
			session_start();
		}
		$this->load->model('Admin_mdl','admin');
	}

	public function login(){
		if( empty($_POST) ){
			$this->load->view('admin/login/login');
			return ;
		}
		$username = $this->input->post_get('user');
		$password = $this->input->post_get('pwd');
		$code     = $this->input->post_get('code');
		
		if( empty($username) || empty($password) || empty($code) ){
			alert('请将用户名,密码,验证码填写完整!',ADMIN_DIR.'login/login');
			exit;
		}
		if( empty($_SESSION[self::CAPTCHA_SESSION_KEY]) || strtolower($code) != strtolower($_SESSION[self::CAPTCHA_SESSION_KEY]) ){
			alert('验证码错误!',ADMIN_DIR.'login/login');
			exit;
		}

		$user = $this->admin->login($username,$password);
		if( !$user ){
			alert('用户名或密码错误',ADMIN_DIR.'login/login');
			exit;
		}
		// $this->_success($user);
		redirect(ADMIN_DIR.'main/index');
	}

	public function logout(){
		$this->admin->logout();
		alert('退出成功!欢迎再次登陆!',ADMIN_DIR.'login/login');
	}

	/**
	 * 验证码
	 */
	public function code(){
		$config = array(
				'width'	=>	115,
				'height'=>	46,
				'codeLen'=>	4,
				'fontSize'=>24,
				'sessionKey'=>self::CAPTCHA_SESSION_KEY,
		);
		$this->load->library('ImgCaptcha', $config,'ImgCaptcha');
		$this->ImgCaptcha->show();
	}
}
